﻿#include "ubuttongroup.h"

#include <QPainter>
#include <QMouseEvent>

const int margin = 3;

class UButtonGroupPrivate
{
public:
    UButtonGroupPrivate(UButtonGroup* q) : q_ptr(q) {}
    ~UButtonGroupPrivate() {}
private:
    Q_DECLARE_PUBLIC(UButtonGroup)
    Q_DISABLE_COPY(UButtonGroupPrivate)
    UButtonGroup* const q_ptr = nullptr;

    QColor background = QColor();
    QColor buttonColor = QColor();
    QStringList textList;
    bool isChecked = false;
    QVector<QRect> rectVec = {};
    int index = 0;
};

UButtonGroup::UButtonGroup(QWidget* parent)
    : QFrame(parent), d_ptr(new UButtonGroupPrivate(this))
{
}

UButtonGroup::~UButtonGroup()
{
}

void UButtonGroup::setBackgroudColor(const QColor &color)
{
    Q_D(UButtonGroup);
    if (d->background != color)
    {
        d->background = color;
        update();
    }
}

void UButtonGroup::setButtonColor(const QColor &color)
{
    Q_D(UButtonGroup);
    if (d->buttonColor != color)
    {
        d->buttonColor = color;
        update();
    }
}

void UButtonGroup::setTexts(const QStringList &list)
{
    Q_D(UButtonGroup);
    d->textList = list;
    update();
}

int UButtonGroup::index()
{
    Q_D(UButtonGroup);
    return d->index;
}

void UButtonGroup::setIndex(int index)
{
    Q_D(UButtonGroup);
    d->index = index;
    update();
}

void UButtonGroup::paintEvent(QPaintEvent*)
{
    Q_D(UButtonGroup);
    QPainter painter(this);
    painter.setRenderHints(QPainter::Antialiasing, true);
    isEnabled() ? painter.setOpacity(1) : painter.setOpacity(0.4);

    painter.save();
    painter.setPen(Qt::NoPen);
    painter.setBrush(d->background.isValid() ? d->background : palette().color(QPalette::Button).darker(120));
    painter.drawRoundedRect(QRect(0, 0, width(), height()), 6, 6);
    painter.setBrush(d->buttonColor.isValid() ? d->buttonColor : palette().color(QPalette::Button));

    int thumbW = (width() - margin * 2) / d->textList.size();
    int thumbH = height() - margin * 2;
    painter.drawRoundedRect(QRect(margin + (d->index * thumbW), margin, thumbW, thumbH), 4, 4);
    painter.restore();

    d->rectVec.clear();
    painter.setPen(palette().color(QPalette::ButtonText));
    for (int i = 0; i < d->textList.size(); i++)
    {
        auto rect = QRect(margin + (i * thumbW), 0, (width() - margin * 2) / d->textList.size(), height());
        d->rectVec.append(rect);
        painter.drawText(rect, Qt::AlignCenter, d->textList.at(i));
    }
}

void UButtonGroup::mouseReleaseEvent(QMouseEvent* e)
{
    Q_D(UButtonGroup);
    if (Qt::LeftButton == e->button())
    {
        QPoint point = e->pos();
        for (int i = 0; i < d->rectVec.size(); i++)
        {
            QRect rect = d->rectVec.at(i);
            if (rect.contains(point))
            {
                d->index = i;
                emit buttonCliked(i);
                break;
            }
        }
    }
    update();
}

QSize UButtonGroup::sizeHint() const
{
    Q_D(const UButtonGroup);
    int width = 0;
    QFontMetrics fontMetrics(font());
    for (const auto &text : d->textList)
    {
        width += fontMetrics.horizontalAdvance(text) + 16;
    }
    return QSize(width, 28);
}
